package com.xm.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wxm.servlet.BaseServlet;
import com.xm.dao.MessageDaoImpl;
import com.xm.entity.Message;

import com.xm.uti.Result;

/**
 * Servlet implementation class MessageServletAdmin
 */
@WebServlet("/admin/MessageServletAdmin")
public class MessageServletAdmin extends BaseServlet {
	MessageDaoImpl mess = new MessageDaoImpl();

	/**
	 * 获取留言板列表
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getmessages(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.从页面上拿到数据
		String name = req.getParameter("name");// 表单中标签nameֵ
		String state = req.getParameter("state");
		String mindate = req.getParameter("mindate");
		String maxdate = req.getParameter("maxdate");
		String page = req.getParameter("page");
		String limit = req.getParameter("limit");
		// 2、调用dao层方法获取数据分页查询到数据中
		ArrayList<Message> msgList = mess.getmessage(name, state, mindate, maxdate, page, limit);

		ArrayList<Message> msgList2 = mess.getmessage(name, state, mindate, maxdate, null, null);

		// 3、讲数据返回到页面上
		String res = Result.toClient(0, "分页查询", msgList, msgList2.size());
		PrintWriter out = resp.getWriter();
		out.print(res);
		out.flush();// 刷新
		out.close();// 关闭
	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updatemessage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.从页面上拿到数据
		String remark = req.getParameter("remark");// 表单中标签nameֵ
		String id = req.getParameter("id");
		String state = req.getParameter("state");

		int ids = 0;
		if (id != null && !id.equals("")) {
			ids = Integer.valueOf(id);
		}

		// 2、调用dao层方法获取数据修改到数据中
		Message msg = new Message(ids, state, remark);
		boolean res = mess.updateMessage(msg);
		// 3、讲数据返回到页面上
		PrintWriter out = resp.getWriter();
		if (res) {
			out.write(Result.toClient(0, "修改成功"));
		} else {
			out.write(Result.toClient(1, "修改失败"));
		}
		out.flush();// 刷新
		out.close();// 关闭
	}

	/**
	 * 删除
	 */
	public void delMessage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.从页面上拿到数据
		String id = req.getParameter("id");// 表单中标签name

		int ids = 0;
		if (id != null && !id.equals("")) {
			ids = Integer.valueOf(id);
		}

		// 2、调用dao层方法获取数据删除
		boolean res = mess.delmessage(ids);

		// 3、讲数据返回到页面上
		PrintWriter out = resp.getWriter();
		if (res) {
			out.write(Result.toClient(0, "删除成功"));
		} else {
			out.write(Result.toClient(1, "删除失败"));
		}
		out.flush();// 刷新
		out.close();// 关闭
	}
}
